package lysjmaster.base.common.conver;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lysjmaster.base.common.web.ParamUtil;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author Created by zk on 2019-08-05 11:26
 * @description 小数转成百分率--给前端
 */
public class PercentageJsonSerializer extends JsonSerializer<BigDecimal> {
    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        if(ParamUtil.isBlank(bigDecimal)){
            jsonGenerator.writeString("0");
        }else {
            jsonGenerator.writeString(bigDecimal.multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString());
        }
    }
}
